package com.bose.bosesystemservice;

import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.Log;

import com.bose.IBoseCmd;


public class BoseSystemService extends IntentService {
    private static final String TAG = "BoseSystemService";

    public static final String ACTION_CALL_SYSTEM = "com.bose.action.CALL_SYSTEM";
    public static final String ACTION_CALL_POPEN = "com.bose.action.CALL_POPEN";
    public static final String EXTRA_PARAM_CMD_STRING = "com.bose.extra.PARAM_CMD_STRING";

    public static final String EXTRA_PARAM_TOKEN = "com.bose.extra.PARAM_TOKEN";

    private IBoseCmd mBoseAidl;


    public BoseSystemService() {
        super("BoseSystemService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_CALL_SYSTEM.equals(action)) {
                final String cmd = intent.getStringExtra(EXTRA_PARAM_CMD_STRING);
                final int token = intent.getIntExtra(EXTRA_PARAM_TOKEN, -1);
                handleActionSystem(cmd, token);
            } else if (ACTION_CALL_POPEN.equals(action)) {
                final String cmd = intent.getStringExtra(EXTRA_PARAM_CMD_STRING);
                final int token = intent.getIntExtra(EXTRA_PARAM_TOKEN, -1);
                handleActionPopen(cmd, token);
            }
        }
    }
    
    private void handleActionSystem(String cmd, int token) {
        int ret = CallSystem(cmd);
        broadcastResult(token, ret, null);
    }
    
    private void handleActionPopen(String cmd, int token) {
        int ret = -11;
        String result = CallPopen(cmd, ret);
        broadcastResult(token, ret, result);
    }


    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "onServiceConnected");
            mBoseAidl = IBoseCmd.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBoseAidl = null;
        }
    };

     private void registerSysService() {
        IBinder binder = null;
        binder  = android.os.ServiceManager.getService("bosecmd_sys");
        if (null == binder) {
             Log.d(TAG, "bosecmd_sys is null");
        } else {
            mBoseAidl = IBoseCmd.Stub.asInterface(binder);
        }
     }

      /* 
      need to add item below in file framework/base/core/res/AndroidManifest.xml
      <protected-broadcast android:name="com.bose.Bosecmd_result" />
      */
     private void broadcastResult(int token, int ret, String result) {
        Intent intent = new Intent();
        intent.setAction("com.bose.bosecmd_result");
        intent.putExtra("token", token);
        intent.putExtra("errorcode", ret);
        if (null != result) {
            intent.putExtra("errormessage", result);
        }
        sendBroadcastAsUser(intent, UserHandle.ALL);
    }

    public int CallSystem(String cmd) {
        int ret = -10;
        if(null == mBoseAidl) {
            registerSysService();
        }

        if (mBoseAidl != null) {
            try {
                ret = mBoseAidl.CallSystem(cmd);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    public String CallPopen(String cmd, int err) {
        byte[] result = null;
        int[] errCode = new int[1];
        errCode[0] = 0;
        if(null == mBoseAidl) {
            registerSysService();
        }

        if (mBoseAidl != null) {
            try {
                result = mBoseAidl.CallPopen(cmd, errCode);
            } catch (RemoteException e) {
                Log.e(TAG, "CallPopen() RemoteException.");
                e.printStackTrace();
            }
        } else {
            Log.e(TAG, "mBoseAidl is null");
        }
        err = errCode[0];
        if (null != result) {
            return new String(result);
        } else {
            Log.e(TAG, "CallPopen() result is null!");
            return null;
        }
    }
}